
package lwf.handler.entities;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.lwf.number.util.NumberFormatter;

import lwf.handler.ProcessHandler;


public class Score {
	
	private long correctQs = 0;
	private long wrongQs = 0;
	private double score = 0;
	private long percentage = 0;
	private List data;
	private Map sumdata;
	private int cumulativetime = 0;
	
	public Score(Map _sumdata) {
		sumdata = _sumdata;
		this.processResults();
	}
	
	public Score() {
		
	}
	
	public void addCorrectQ() {
		correctQs++;
	}
	
	public void addWrongQ() {
		wrongQs++;
	}
	
	private void addScore(String _score) {
		score = score + Double.parseDouble(_score);
	}

	/**
	 * @return Returns the correctQs.
	 */
	public long getCorrectQs1() {
		return correctQs;
	}
	
	public String getCorrectQs() {
		return String.valueOf(correctQs);
	}
	
	/**
	 * @return Returns the percentage.
	 */
	public String getPercentage() {
		double percentage = ((score)/(correctQs+wrongQs)) * 100;
		return NumberFormatter.format(String.valueOf(percentage), "#.##"); 
		//return String.valueOf(percentage);
	}
	/**
	 * @return Returns the score.
	 */
	public String getScore() {
		return NumberFormatter.format(String.valueOf(score), "#.#");
	}
	/**
	 * @return Returns the wrongQs.
	 */
	public String getWrongQs() {
		return String.valueOf(wrongQs);
	}
	
	public long getWrongQs1() {
		return wrongQs;
	}
	
	/**
	 * @return Returns the data.
	 */
	public List getData() {
		return data;
	}
	/**
	 * @param data The data to set.
	 */
	public void setData(List data) {
		this.data = data;
	}
	
	private void processResults() {
		if(sumdata != null && sumdata.size() != 0) {
			Iterator itr = sumdata.entrySet().iterator();
			while(itr.hasNext()) {
				Map.Entry mapentry = (Map.Entry) itr.next();
				ProcessHandler sumdetail = (ProcessHandler) mapentry.getValue();
				if(sumdetail.isAnswered()) {
					if(data == null) {
						data = new ArrayList();
					}
					data.add(sumdetail);	
					if(sumdetail.getCorrectAnswer()) {
						addCorrectQ();
						addScore(sumdetail.getScore());
					}
					else {
						addWrongQ();
					}
					cumulativetime = cumulativetime + Integer.parseInt(sumdetail.getTimeTaken());
				}
			}
		}		
	}	
	
	public String getTotalTime() {
		return String.valueOf(cumulativetime/60) + " minutes";
	}
} //Score
